/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.table.cellEditor;

import java.awt.Component;
import java.math.BigDecimal;

import javax.swing.DefaultCellEditor;
import javax.swing.JTable;

import org.jxSwing.component.text.NumberField;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.table.cellEditor
 * @date 31/03/2014 - 22:00:34
 */
public class NumericEditor extends DefaultCellEditor {
	
	private static final long serialVersionUID = 5966084864935629418L;
	
	/**
	 * @param currency
	 */
	public NumericEditor() {
	
		super(new NumberField());
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.DefaultCellEditor#getCellEditorValue()
	 */
	@Override
	public BigDecimal getCellEditorValue() {
	
		return getComponent().getBigDecimalValue();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.DefaultCellEditor#getComponent()
	 */
	@Override
	public NumberField getComponent() {
	
		return (NumberField)super.getComponent();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.DefaultCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
	 */
	@Override
	public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
	
		//
		if(value != null) {
			
			String strValue = value.toString().trim();
			
			final int decimalDigits = getComponent().getDecimalDigits() > 0 ? getComponent().getDecimalDigits() : 0;
			
			strValue = String.valueOf(new BigDecimal(strValue).setScale(decimalDigits, BigDecimal.ROUND_CEILING));
			
			char separator = '\0';
			
			//
			for(int index = 0; index < strValue.length(); index++) {
				
				//
				if( !Character.isDigit(strValue.charAt(index)) && strValue.charAt(index) != '-') {
					
					separator = strValue.charAt(index);
					
					break;
				}
			}
			
			getComponent().setText(strValue.replace(separator, getComponent().getDecimalSeparator()));
			
		}else {
			
			getComponent().setText("");
		}
		
		return getComponent();
	}
	
	/**
	 * @param decimalDigits
	 */
	public void setDecimalDigits(final int decimalDigits) {
	
		getComponent().setDecimalDigits(decimalDigits);
	}
	
	/**
	 * @param money
	 */
	public void setMoney(final boolean money) {
	
		getComponent().setMoney(money);
	}
}
